/*
 * Copyright 2007-2008  Lukasz Laszko lukaszlaszko@gmail.com
 * 
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and this
 * permission notice and warranty disclaimer appear in supporting
 * documentation, and that the name of the author not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * 
 * The author disclaim all warranties with regard to this
 * software, including all implied warranties of merchantability
 * and fitness.  In no event shall the author be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether
 * in an action of contract, negligence or other tortious action,
 * arising out of or in connection with the use or performance of
 * this software. 
 */
package org.pg.biomedics.asr.ws;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

import org.apache.log4j.Logger;

import org.pg.biomedics.asr.service.ServicesPool;
import org.pg.biomedics.asr.service.CommonService;
import org.pg.biomedics.asr.service.ServiceException;
import org.pg.biomedics.asr.model.Task;
import org.pg.biomedics.asr.model.Result;
import org.pg.biomedics.asr.model.TaskHistoryEntry;
import org.pg.biomedics.asr.concurrent.TaskScheduler;

/**
 * ASR web service interface
 * @author Lukasz Laszko
 */
@WebService
public class ASRService 
{
    // <editor-fold desc="Fields">
    
    private Logger logger = Logger.getLogger(ASRService.class);
    
    // </editor-fold>
    
    // <editor-fold desc="Methods">
    
    @WebMethod(operationName="EnqueueTask")
    public Task enqueueTask(@WebParam(name="name") String name, 
                            @WebParam(name="type") String type, 
                            @WebParam(name="priority") String priority, 
                            @WebParam(name="media") byte[] media)
            throws Exception
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            TaskScheduler taskScheduler = ServicesPool.getInstance().getTaskScheduler();
            
            Task enqueuedTask = commonService.enqueueTask(name, type, priority, media);            
            taskScheduler.submit(enqueuedTask);
            
            return enqueuedTask;
        }
        catch (Exception ex)
        {            
            logger.error("enqueTask failed", ex);
            throw ex;
        }
    }
    
    @WebMethod(operationName="GetTask")
    public Task getTask(@WebParam(name="taskUniqueId") String taskUniqueId) throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.getTask(taskUniqueId);
        }
        catch (ServiceException ex)
        {            
            logger.error("checkTaskStatus failed", ex);
            throw ex;
        }
    }
    
    @WebMethod(operationName="CheckTaskStatus")
    public String checkTaskStatus(@WebParam(name="taskUniqueId") String taskUniqueId) throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.checkTaskStatus(taskUniqueId);
        }
        catch (ServiceException ex)
        {            
            logger.error("checkTaskStatus failed", ex);
            throw ex;
        }
    }   
    
    @WebMethod(operationName="GetWaitingTasks")
    public Task[] getWaitingTasks() throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.getWaitingTasks();
        }
        catch (ServiceException ex)
        {            
            logger.error("getWaitingTasks failed", ex);
            throw ex;
        }
    }
    
    @WebMethod(operationName="GetPendingTasks")
    public Task[] getPendingTasks() throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.getPendingTasks();
        }
        catch (ServiceException ex)
        {            
            logger.error("getPendingTasks failed", ex);
            throw ex;
        }
    }
    
    @WebMethod(operationName="GetEnqueuedTasks")
    public Task[] getEnqueuedTasks() throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.getEnqueuedTasks();
        }
        catch (ServiceException ex)
        {            
            logger.error("getEnqueuedTasks failed", ex);
            throw ex;
        }
    }
    
    @WebMethod(operationName="GetResultingTasks")
    public Task[] getResultingTasks() throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.getResultingTasks();
        }
        catch (ServiceException ex)
        {            
            logger.error("getResultingTasks failed", ex);
            throw ex;
        }
    }
    
    @WebMethod(operationName="FinishTask")
    public Task finishTask(@WebParam(name="task") Task task) throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.finishTask(task);
        }
        catch (ServiceException ex)
        {            
            logger.error("finishTask failed", ex);
            throw ex;
        }
    }
    
    @WebMethod(operationName="CancelTask")
    public Task cancelTask(@WebParam(name="task") Task task) throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.cancelTask(task);
        }
        catch (ServiceException ex)
        {            
            logger.error("cancelTask failed", ex);
            throw ex;
        }
    }
    
    @WebMethod(operationName="GetResults")
    public Result[] getResults(@WebParam(name="task") Task task) throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.getResults(task);
        }
        catch (ServiceException ex)
        {            
            logger.error("getResults failed", ex);
            throw ex;
        }
    }
    
    @WebMethod(operationName="GetHistory")
    public TaskHistoryEntry[] getHistory(@WebParam(name="task") Task task) throws ServiceException
    {
        try
        {
            CommonService commonService = ServicesPool.getInstance().getCommonService();
            return commonService.getHistory(task);
        }
        catch (ServiceException ex)
        {            
            logger.error("getHistory failed", ex);
            throw ex;
        }
    }
    
    // </editor-fold>
}
